#ifndef APPCONFIG_H
#define APPCONFIG_H

#include <nlohmann/json.hpp>
#include <string>
#include <fstream>
#include <iostream>

struct AppConfig{
    int stn1{8};
    int stn2{9} ;
    int up{9} ;
    int down{9} ;
    int assm{10} ;
    int Wrl_start{30};
    int moniCount{0};

    friend void to_json(const std::string& filename, const AppConfig& wrl) {
        auto j = nlohmann::json{
            {"stn1", wrl.stn1},
            {"stn2", wrl.stn2},
                {"assm", wrl.assm},
                {"up", wrl.up},
                {"down", wrl.down},
                {"moniCount", wrl.moniCount},
            {"Wrl_start", wrl.Wrl_start}
        };

        std::ofstream file(filename);
        if (file.is_open()) {
            file << j.dump(4);  // 格式化输出 JSON
            file.close();
            std::cout << "AppConfig serialized and written to " << filename << std::endl;
        } else {
            std::cerr << "AppConfig 无法写入Json!" << std::endl;
        }
    }

    friend void from_json(const std::string& filename, AppConfig& wrl) {

        std::ifstream file(filename);
        if (file.is_open()) {
            nlohmann::json j;
            file >> j;
            file.close();

            j.at("stn1").get_to(wrl.stn1);
            j.at("assm").get_to(wrl.assm);
            j.at("up").get_to(wrl.up);
            j.at("down").get_to(wrl.down);
            j.at("stn2").get_to(wrl.stn2);
            j.at("moniCount").get_to(wrl.moniCount);
            j.at("Wrl_start").get_to(wrl.Wrl_start);

        } else {
            std::cerr << "AppConfig 无法Read Json!!" << std::endl;
        }

    }
};


#endif // APPCONFIG_H
